var prefix = jsctx + "/tenantCompanyInfo";

var tenantCompanyInfo = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        }, 10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });
    var h = $(window).height() - 80;

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listPageTenantCompanyInfo",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    cache: false,
                    pageSize: 20,
                    pageList: [20],
                    pageNumber: 1,
                    sidePagination: "server",
                    paginationShowPageGo: true,
                    height: h,
                    // rightFixedColumns: true,
                    // showColumns: true,
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            //elecStationId: $("#elecStationId").val().trim(),
                            //  operateStatus: $("#operateStatus").val().trim(),
                        };
                    },
                    columns: [
                        {
                            title: '#', field: '', width: '50',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {field: 'companyName', title: '子公司名称', align: 'left', width: '100'},
                        {field: 'companyTaxNumber', title: '税号', align: 'left', width: '100'},
                        {
                            field: 'invoiceType', title: '开票类型', width: '100',
                            align: 'center',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '一般纳税人'
                                } else if (value == '1') {
                                    return '小规模纳税人'
                                }
                            }
                        },
                        {field: 'companyAddress', title: '地址', align: 'left', width: '100'},
                        {field: 'companyTel', title: '电话', align: 'left', width: '100'},
                        {field: 'companyBank', title: '开户行', align: 'left', width: '100'},
                        {field: 'companyBankAccount', title: '账号', align: 'left', width: '100'},
                        {
                            title: '操作',
                            field: 'roleId',
                            align: 'center',
                            width: '100',
                            formatter: function (value, row, index) {
                                var a = '';
                                var b = '';
                                var c = '';
                                if (Tenant_subcompany_look) {
                                    a = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="详情" onclick="tenantCompanyInfo.detailTenantCompanyInfo(\''
                                        + row.tenantCompanyId
                                        + '\')">详情</a> ';
                                }
                                if (Tenant_subcompany_update) {
                                    b = '<a class="btn btn-danger btn-sm ' + has_edit + '" href="#" title="编辑" onclick="tenantCompanyInfo.editTenantCompanyInfo(\''
                                        + row.tenantCompanyId
                                        + '\')">编辑</a> ';
                                }
                                if (Tenant_subcompany_delete) {
                                    c = '<a class="btn btn-danger btn-sm ' + has_edit + '" href="#" title="删除" onclick="tenantCompanyInfo.deleteTenantCompanyInfo(\''
                                        + row.tenantCompanyId
                                        + '\')">删除</a> ';
                                }
                                return a + b + c;
                            }
                        }
                    ]
                });
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }

    function addTenantCompanyInfo(tenantCompanyId) {
        location.href = prefix + "/addTenantCompanyInfo";
    }

    function editTenantCompanyInfo(tenantCompanyId) {
        location.href = prefix + "/editTenantCompanyInfo/" + tenantCompanyId;
    }

    function detailTenantCompanyInfo(tenantCompanyId) {
        location.href = prefix + "/detailTenantCompanyInfo/" + tenantCompanyId;
    }

    function deleteTenantCompanyInfo(tenantCompanyId) {
        layer.confirm('删除操作将无法撤回,真的要删除吗？', {
            btn: ['是的', '不了']
        }, function (index) {
            $.ajax({
                url: prefix + "/deleteTenantCompanyInfo",
                type: "post",
                data: {
                    'tenantCompanyId': tenantCompanyId,
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("删除成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }


    function goBack() {
        window.history.back();
    }


    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        addTenantCompanyInfo: addTenantCompanyInfo,
        editTenantCompanyInfo: editTenantCompanyInfo,
        deleteTenantCompanyInfo: deleteTenantCompanyInfo,
        detailTenantCompanyInfo: detailTenantCompanyInfo
    };
})();

var addTenantCompanyInfo = (function () {
    function init() {
        validateRule();
    };

    function getCheckedRoles() {
        var adIds = "";
        $("input:checkbox[name=role]:checked").each(function (i) {
            if (0 == i) {
                adIds = $(this).val();
            } else {
                adIds += ("," + $(this).val());
            }
        });
        return adIds;
    }

    var isajaxsend = true;

    function save() {
        if (isajaxsend) {
            var formData = new FormData($("#frmTenantCompanyInfo")[0]);
            $("#roleIds").val(getCheckedRoles());
            $.ajax({
                cache: true,
                type: "POST",
                url: prefix + "/saveOrUpdateTenantCompanyInfo",
                data: formData,
                processData: false,
                contentType: false,
                async: false,
                beforeSend: function () {
                    isajaxsend = false
                },
                error: function (request) {
                    parent.layer.alert("Connection error");
                    isajaxsend = true;
                },
                success: function (data) {
                    if (data.status == 200) {
                        isajaxsend = false
                        parent.layer.msg("操作成功");
                        window.history.back();
                    } else {
                        parent.layer.alert(data.message)
                        isajaxsend = true;
                    }

                }
            });
        }
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frmTenantCompanyInfo").validate({
            rules: {
                companyName: {
                    required: true,
                    maxlength: 50
                },
                companyTaxNumber: {
                    required: true,
                    maxlength: 50
                },
                companyBank: {
                    required: true,
                    maxlength: 50
                },
                companyBankAccount: {
                    required: true,
                    maxlength: 50
                },
                companyAddress: {
                    required: true,
                    maxlength: 50
                },
                companyTel: {
                    required: true,
                    maxlength: 50,
                    digits: true,
                }

            },
            messages: {
                companyName: {
                    required: icon + "请输入子公司名称",
                    maxlength: icon + "子公司名称不大于50字",
                },
                companyTaxNumber: {
                    required: icon + "请输入税号",
                    maxlength: icon + "税号不大于50字"
                },
                companyBank: {
                    required: icon + "请输入开户行",
                    maxlength: icon + "开户行不大于50字"
                },
                companyBankAccount: {
                    required: icon + "请输入账号",
                    maxlength: icon + "账号不大于50字"
                },
                companyAddress: {
                    required: icon + "请输入地址",
                    maxlength: icon + "地址不大于50字"
                },
                companyTel: {
                    required: icon + "请输入电话",
                    maxlength: icon + "电话不大于50字",
                    digits: icon + "电话号格式不正确"
                }
            },
            submitHandler: function (form) {
                save();
            }
        })
    }

    return {
        init: init
    };
})();
